package com.sinoglobal.wallet.adapter;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.bitmap.callback.DefaultBitmapLoadCallBack;
import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.activity.W_OutDateCouponActivity;
import com.sinoglobal.wallet.activity.W_ShareAbstractActivity;
import com.sinoglobal.wallet.activity.W_SinoBaseActivity;
import com.sinoglobal.wallet.app.SinoValueManager;
import com.sinoglobal.wallet.entity.W_CouponListEntity.CouponList;
import android.graphics.PorterDuff;
public class W_CouponAdapter extends W_SinoBaseSimpleAdapter<CouponList>{
  private  Context context;
private BitmapUtils bitmapUtils;
	public W_CouponAdapter(Context mContext) {
		super(mContext);
		this.context= mContext;
	   bitmapUtils = new BitmapUtils(mContext);
      	bitmapUtils.configDefaultLoadingImage(R.drawable.wallet_coupon_default_img);
	    bitmapUtils.configDefaultLoadFailedImage(R.drawable.wallet_coupon_default_img);
	}
	@Override
	public void findViews(Map<java.lang.String, View> widgets, int position, View convertView) {
		TextView wallet_coupon_name = (TextView) convertView.findViewById(R.id.wallet_coupon_name);
		TextView wallet_coupon_share_txt = (TextView) convertView.findViewById(R.id.wallet_coupon_share_txt);
		TextView wallet_coupon_name_source = (TextView) convertView.findViewById(R.id.wallet_coupon_name_source);
		TextView wallet_coupon_apply_scope = (TextView) convertView.findViewById(R.id.wallet_coupon_apply_scope);
		TextView wallet_coupon_time = (TextView) convertView.findViewById(R.id.wallet_coupon_time);
		TextView wallet_coupon_outdate_staytime = (TextView) convertView.findViewById(R.id.wallet_coupon_outdate_staytime);
		TextView wallet_coupon_outdate = (TextView) convertView.findViewById(R.id.wallet_coupon_outdate);
		ImageView wallet_coupon_img = (ImageView) convertView.findViewById(R.id.wallet_coupon_img);
		LinearLayout wallet_coupon_share_background_layout = (LinearLayout) convertView.findViewById(R.id.wallet_coupon_share_background_layout);
		widgets.put("wallet_coupon_img", wallet_coupon_img);
		widgets.put("wallet_coupon_name", wallet_coupon_name);
		widgets.put("wallet_coupon_share_txt", wallet_coupon_share_txt);
		widgets.put("wallet_coupon_name_source", wallet_coupon_name_source);
		widgets.put("wallet_coupon_apply_scope", wallet_coupon_apply_scope);
		widgets.put("wallet_coupon_time", wallet_coupon_time);
		widgets.put("wallet_coupon_outdate_staytime", wallet_coupon_outdate_staytime);
		widgets.put("wallet_coupon_share_background_layout", wallet_coupon_share_background_layout);
		widgets.put("wallet_coupon_outdate", wallet_coupon_outdate);
	}
	

	@Override
	public void setValues(View convertView, ViewHolder holder, Map<String, View> widgets, int position) {
		TextView wallet_coupon_name = (TextView) widgets.get("wallet_coupon_name");
		TextView wallet_coupon_name_source = (TextView) widgets.get("wallet_coupon_name_source");
		TextView wallet_coupon_outdate = (TextView) widgets.get("wallet_coupon_outdate");
		TextView wallet_coupon_apply_scope = (TextView) widgets.get("wallet_coupon_apply_scope");
		TextView wallet_coupon_share_txt = (TextView) widgets.get("wallet_coupon_share_txt");
		TextView wallet_coupon_time = (TextView) widgets.get("wallet_coupon_time");
		ImageView wallet_coupon_img = (ImageView) widgets.get("wallet_coupon_img");
		TextView wallet_coupon_outdate_staytime = (TextView) widgets.get("wallet_coupon_outdate_staytime");
		LinearLayout wallet_coupon_share_background_layout = (LinearLayout) widgets.get("wallet_coupon_share_background_layout");
	
		if(mDatas.size()==(position+1)){
			wallet_coupon_outdate.setVisibility(View.VISIBLE);
			if( context instanceof W_OutDateCouponActivity){
				wallet_coupon_outdate.setVisibility(View.GONE);
			}
		}else{
			wallet_coupon_outdate.setVisibility(View.GONE);
		}
		if(position==0){
			if( context instanceof W_OutDateCouponActivity){
				wallet_coupon_outdate_staytime.setVisibility(View.VISIBLE);
			}
		}else{
			wallet_coupon_outdate_staytime.setVisibility(View.GONE);
		}
		final CouponList couponItem = (CouponList) getItem(position);
		
		wallet_coupon_name.setText(couponItem.getCOUPON_NAME());
		wallet_coupon_apply_scope.setText(couponItem.getAPPLY_SCOPE());
		wallet_coupon_time.setText(couponItem.getSTART_DATE()+"——"+couponItem.getEND_DATE());
		wallet_coupon_name_source.setText(couponItem.getTHIRD_PARTY_NAME());
		
	    bitmapUtils.display(wallet_coupon_img, couponItem.getLOGO_IMAGE(),new BitmapCallBack());
	    wallet_coupon_outdate.setText(Html.fromHtml("<u>" + "已过期优惠券" + "</u>"));
		if( context instanceof W_OutDateCouponActivity){
			wallet_coupon_name.setTextColor(context.getResources().getColor(R.color.w_payPass_graytwo));
			wallet_coupon_apply_scope.setTextColor(context.getResources().getColor(R.color.w_payPass_gray));
			wallet_coupon_time.setTextColor(context.getResources().getColor(R.color.w_gray_three));
			wallet_coupon_name_source.setTextColor(context.getResources().getColor(R.color.w_payPass_gray));
		}
		final int state=couponItem.getUSE_STATE();
		
		if(state==0){
			wallet_coupon_share_txt.setText("立即分享");
			wallet_coupon_share_background_layout.setBackgroundResource(R.drawable.wallet_coupon_share);
		}else if(state==1){
			wallet_coupon_share_txt.setText("已使用");
			wallet_coupon_share_background_layout.setBackgroundResource(R.drawable.wallet_coupon_share_blue_img);
		}else if (state==2){
			wallet_coupon_share_txt.setText("已过期");
			wallet_coupon_share_background_layout.setBackgroundResource(R.drawable.wallet_coupon_share_gray);
		}
		wallet_coupon_outdate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			((W_SinoBaseActivity) context).goIntent(W_OutDateCouponActivity.class);
			}
		});
		wallet_coupon_share_background_layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String shareStr=couponItem.getSHARE_LINK()+"/?productCode="+ SinoValueManager.getProductID(context);
				if(state==0){
					 Bitmap  bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.wallet_coupon_share_default_img);			
					 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
						bitmap.compress(CompressFormat.JPEG,60, outputStream);
						Bitmap shareBitmap=compressImageFromByte(outputStream.toByteArray());
					if(shareBitmap!=null){
						((W_ShareAbstractActivity) context).setWalletShare("我在"+context.getResources().getString(R.string.w_main_app_name)+"的活动中抢了张"+couponItem.getCOUPON_NAME()+"，你也快来抢吧!", null, shareStr, shareBitmap);
					}else{
						((W_ShareAbstractActivity) context).showToast("分享数据加载中...");
					
					}
				
				}
			}
		});
	
	}
	class BitmapCallBack extends DefaultBitmapLoadCallBack<ImageView>{
		@Override
		public void onLoadFailed(ImageView container, String uri,
				Drawable drawable) {
			super.onLoadFailed(container, uri, drawable);
			 Bitmap  bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.wallet_coupon_share_default_img);			
			 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				bitmap.compress(CompressFormat.JPEG,60, outputStream);
		}
		@Override
		public void onLoadCompleted(ImageView container, String uri,Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
			super.onLoadCompleted(container, uri, bitmap, config, from);
			if( context instanceof W_OutDateCouponActivity){
				Drawable drawable =new BitmapDrawable(bitmap);   
				drawable.setColorFilter(Color.GRAY,PorterDuff.Mode.MULTIPLY);;
				container.setImageDrawable(drawable);
			}
			//ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			//bitmap.compress(CompressFormat.JPEG, 60, outputStream);
			//shareBitmap=compressImageFromByte(outputStream.toByteArray());
		}
	

	}
	@Override
	public View setAdapterLayout(LayoutInflater inflater, int position) {
		return inflater.inflate(R.layout.wallet_coupon_listitem_layout, null);
	}
	private Bitmap compressImageFromByte(byte[] array) {
	    BitmapFactory.Options newOpts = new BitmapFactory.Options();
	    newOpts.inJustDecodeBounds = true;//只读边,不读内容
	    Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length, newOpts);

	    newOpts.inJustDecodeBounds = false;
	    int w = newOpts.outWidth;
	    int h = newOpts.outHeight;
	    float hh = 95f;//
	    float ww = 95f;//
	    int be = 1;
	    if (w > h && w > ww) {
	      be = (int) (newOpts.outWidth / ww);
	    } else if (w < h && h > hh) {
	      be = (int) (newOpts.outHeight / hh);
	    }
	    if (be <= 0)
	      be = 1;
	    newOpts.inSampleSize = be;//设置采样率
	    
	    newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设
	    newOpts.inPurgeable = true;// 同时设置才会有效
	    newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
	    
	    bitmap = BitmapFactory.decodeByteArray(array,0,array.length,newOpts);
	    return bitmap;
	  }
}
